import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class NGuessNumber {
    public int[] generateAnswer() {
        int[] answer = new int[4];

        for (int i = 0; i < answer.length; i++) {
            answer[i] = -1;
        }
        int index = 0;

        while (index < answer.length) {
            Random rod = new Random();

            int num = rod.nextInt(10);

            if (judge(answer, num)) {
                answer[index++] = num;
            }
        }

        return answer;
    }

    public static boolean judge(int[] arr, int num) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == num) {
                return false;
            }
        }

        return true;
    }


    public int[] getPlayerInput() {
        int[] guess = new int[4];

        Scanner sc = new Scanner(System.in);

        for (int i = 0; i < 4; i++) {
            int temp = sc.nextInt();

            if (temp >= 0 && temp <= 9) {
                guess[i] = temp;
            }
        }

        System.out.println(Arrays.toString(guess));


        return guess;
    }


    public static int[] compareGuessAnswer(int[] answer, int[] guess) {
        int[] arr_AB = new int[2];

        int A = 0;
        int B = 0;

        for (int i = 0; i < 4; i++) {
            if (guess[i] == answer[i]) {
                A++;
            }
        }

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (guess[i] == answer[j]) {
                    B++;
                }
            }
        }


        arr_AB[0] = A;
        arr_AB[1] = B;

        System.out.println("A = " + arr_AB[0] + ", B = " + arr_AB[1]);

        return arr_AB;
    }

    public static boolean isWin(int[] answer, int[] guess) {
        int A = compareGuessAnswer(answer, guess)[0];

        if (A == 4) {
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
        NGuessNumber nGuessNumber = new NGuessNumber();

        String against = "";
        Scanner sc = new Scanner(System.in);
        do {
            int[] p = nGuessNumber.generateAnswer();
            System.out.println("答案如下:");
            System.out.println(Arrays.toString(p));
            int gamenum = 0;
            while (gamenum < 5) {
                int[] user = nGuessNumber.getPlayerInput();
//        System.out.println(Arrays.toString(compareGuessAnswer(p, user)));
                if (isWin(p, user)) {
                    System.out.println("获胜>>");
                    System.out.println("继续游戏输入y/n");
                    against = sc.next();
                    break;
                } else {
                    System.out.println("再试试>>>");
                }
                gamenum++;
                if (gamenum == 5){
                    System.out.println("5次机会用完啦");
                    System.out.println("继续游戏输入y/n");
                    against = sc.next();

                }
            }

        }while (against.equalsIgnoreCase("y"));





    }
}
